'use strict'

const { Service } = require('egg')
const { Op } = require('sequelize')

class HomeService extends Service {
  /**
   * H5 banner
   * @param {*} param0 
   * @returns 
   */
  async getH5Banner() {
    const { ctx } = this
    const result = await ctx.model.Banner.findAll({
      where: {
        status: 1
      }
    })
    return result
  }

  /**
   * banner列表
   * @returns 
   */
  async getAdminBanner({ pageNo, pageSize, id, desc, status }) {
    const {
      ctx
    } = this
    const dynamicQuery = {
      banner_id: id,
      status
    }
    for (const key in dynamicQuery) { // 过滤空值
      if (Object.hasOwnProperty.call(dynamicQuery, key)) {
        const element = dynamicQuery[key]
        if (!element) delete dynamicQuery[key]
      }
    }
    if (desc) {
      dynamicQuery.desc = {
        [Op.like]: `%${desc}%` // 模糊查询
      }
    }

    const { count, rows } = await ctx.model.Banner.findAndCountAll({
      limit: pageSize * 1,
      offset: (pageNo - 1) * pageSize,
      order: [
        ['createdAt', 'DESC']
      ],
      where: dynamicQuery
    })

    return {
      data: rows,
      pageData: {
        pageNo: pageNo * 1,
        pageSize: pageSize * 1,
        total: count
      }
    }
  }

  /**
   * 轮播图新增/编辑
   * @param {object} banner数据 
   */
  async updateHomeBanner({
    id,
    desc,
    imageUrl,
    sort,
    status
  }) {
    const {
      ctx
    } = this
    let data = {}
    if (desc && imageUrl && sort) {
      data = {
        desc,
        banner_url: imageUrl,
        sort,
      }
    }
    if (!isNaN(status)) {
      data = { status }
    }
    if (!id) { // 新增
      // data['create_time'] = Date.now() // 使用sequelize自动维护的created_at
      await ctx.model.Banner.create(data)
    } else { // 编辑
      // data['update_time'] = Date.now()
      await ctx.model.Banner.update(
        data,
        {
          where: { banner_id: id }
        })
    }
  }

  /**
   * 轮播图删除
   */
  async deleteBanner({ id }) {
    const { ctx } = this
    await ctx.model.Banner.destroy({ where: { banner_id: id } })
  }

  /**
   * h5首页
   */
  async h5home () {
    const { ctx } = this
    
  }

  /**
   * 首页推荐列表
   */
  async getRecommendList({ pageNo, pageSize, recommendId, status, description }) {
    const { ctx } = this
    const dynamicQuery = {
      recommend_id: recommendId,
      status
    }
    for (const key in dynamicQuery) { // 过滤空值
      if (Object.hasOwnProperty.call(dynamicQuery, key)) {
        const element = dynamicQuery[key]
        if (!element) delete dynamicQuery[key]
      }
    }
    if (description) {
      dynamicQuery.description = {
        [Op.like]: `%${description}%` // 模糊查询
      }
    }
    const { count, rows } = await ctx.model.Recommend.findAndCountAll({
      limit: pageSize * 1,
      offset: (pageNo - 1) * pageSize,
      order: [
        ["createdAt", "DESC"]
      ],
      where: dynamicQuery
    })

    return {
      data: rows,
      pageData: {
        pageNo: pageNo * 1,
        pageSize: pageSize * 1,
        total: count
      }
    }
  }

  /**
   * 首页推荐新增、编辑
   * @param {object} param0 
   */
  async updateRecommend(data) {
    const { ctx } = this
    const { recommendId, imageUrl } = data
    data['image_url'] = imageUrl
    if (recommendId) {
      await ctx.model.Recommend.update(data, {
        where: {
          recommend_id: recommendId
        }
      })
    } else {
      await ctx.model.Recommend.create(data)
    }
  }

  /**
   * 推荐详情
   * @param {string} id
   * @returns 
   */
  async recommendDetail (id) {
    const { ctx } = this
    const result = await ctx.model.Recommend.findOne({
      where: {
        recommend_id: id
      }
    })
    return result
  }

  /**
   * 轮播图删除
   */
   async deleteRecommend({ recommendId }) {
    const { ctx } = this
    await ctx.model.Recommend.destroy({ where: { recommend_id: recommendId } })
  }
}

module.exports = HomeService